import { acceptGameApi } from '@/api/client/userApi'
import { useAppStore } from '@/stores/app'
import { useMessageListStore } from '@/stores/messgelist'

export default class AutoAccept {
  constructor() {
    this.loadRunes()
  }

  loadRunes() {
    useMessageListStore().addMessage({
      name: 'ReadyCheck',
      trigger: ({ data }) => {
        this.acceptGame(data)
      },
    })
  }

  async acceptGame(data) {
    // 判断是否启动了自动接受对局
    const { autoAccept, autoAcceptDelay } = useAppStore().setting
    if (!autoAccept) return
    // playerResponse: None无动作/Accepted接受/Declined拒绝
    if (data.playerResponse === 'None') {
      // data.data.timer 倒计时
      if (!autoAcceptDelay || data.timer >= autoAcceptDelay) {
        acceptGameApi()
      }
    }
  }
}
